<?php
/**
 * OS CRM SYSTEM - An open source application development framework for PHP 4.3.2 or newer
 *
 * Model CLASS - DB Model - OS Admin
 * 
 * @name      DB Model
 * @package		OS CRM
 * @category  Class
 * @subcategory  Model
 * @author		CRM SYSTEM Dev Team
 * @copyright	Copyright (c) 2010 - 2012, CRM SYSTEM
 * @since     Version 1.0
 * @filesource
 * @access    public 
 */

// ------------------------------------------------------------------------

class DB_Model extends Model {

  //set db - listing data
  var $e_list_limit     = '10';
  var $e_list_offset    = '0';
  var $e_list_order     = 'asc';
  var $e_list_order_by  = 'os_journals_id';
  var $e_list_id        = 'os_journals_id';
  
  function __construct() {
    // Call the Model constructor
    parent::__construct();
  }

  
  // ------------------------------------------------------------------------
  
  /** 
	 * Backend - Get Journal not mapped with subjects
	 *
	 * @descp  this will get the Journal not mapped with subjects
	 * @access public
	 * @return void
	*/
  function bk_get_jor_not_map() {
    $return_arr  = array();
    
    $qry = "SELECT os_journals_id as `id` FROM ".TBL_JOR_MAP_CAT."";		
    //__debug($qry);
    $return_arr = run_cust_qry($qry);
    $set_id = array();
    if($return_arr['num_rows'] > 0){
      foreach ($return_arr['list'] as $key => $value) {
        $set_id[] = $value['id'];
      }
    }
    $qry = "SELECT count(os_journals_id) as `cnt` FROM ".TBL_JOURNALS." WHERE os_journals_id NOT IN (".  implode(', ', $set_id).") ";			    
    //__debug($qry);
    $return_arr = run_cust_qry($qry,TRUE);
		return $return_arr;
  }
  
  // ------------------------------------------------------------------------
  
  /** 
	 * Backend - Get last month contact cnt
	 *
	 * @descp  this will get the last month contact cnt
	 * @access public
	 * @return void
	*/
  function bk_get_contact_cnt() {
    $return_arr  = array();
    
    $months_back = 1;    
    $dt_from = mktime( date("H", now()), date("i", now()), date("s", now()), (date("m", now()) - $months_back), date("d", now()), date("Y", now()));
    $dt_to = mktime( date("H", now()), date("i", now()), date("s", now()), date("m", now()), date("d", now()), date("Y", now()));
    $data['dt_from'] = standard_date('DATE_OS_CRM_SQL_DT',$dt_from);
    $data['dt_to'] = standard_date('DATE_OS_CRM_SQL_DT',$dt_to);
    
    
    $qry = "SELECT count(os_contact_id) as `cnt` FROM ".TBL_CONTACT_US." 
            WHERE os_contact_created BETWEEN '".$data['dt_from']."' AND '".$data['dt_to']."' ";			    
    //__debug($qry);
    $return_arr = run_cust_qry($qry,TRUE);
		return $return_arr;
  }
  
  
  // ------------------------------------------------------------------------
  
  /** 
	 * Backend - Get last month order cnt
	 *
	 * @descp  this will get the last month order cnt
	 * @access public
	 * @return void
	*/
  function bk_get_order_cnt() {
    $return_arr  = array();
    
    $months_back = 1;    
    $dt_from = mktime( date("H", now()), date("i", now()), date("s", now()), (date("m", now()) - $months_back), date("d", now()), date("Y", now()));
    $dt_to = mktime( date("H", now()), date("i", now()), date("s", now()), date("m", now()), date("d", now()), date("Y", now()));
    $data['dt_from'] = standard_date('DATE_OS_CRM_SQL_DT',$dt_from);
    $data['dt_to'] = standard_date('DATE_OS_CRM_SQL_DT',$dt_to);
    
    
    $qry = "SELECT count(os_ord_usr_id) as `cnt` FROM ".TBL_ORDER_USR." 
            WHERE os_ord_usr_created BETWEEN '".$data['dt_from']."' AND '".$data['dt_to']."' ";			    
    //__debug($qry);
    $return_arr = run_cust_qry($qry,TRUE);
		return $return_arr;
  }
}
?>
